import { createRouter, createWebHistory } from 'vue-router'
import index from '@/views/Index/index.vue'
import { message } from 'ant-design-vue'
import NProgress from'nprogress'
import '/node_modules/nprogress/nprogress.css'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { 
      path: '/',
      name:'index',
      component: index,
      redirect:'/aa',
      children:[{
        path:'aa',
        component:()=>import('@/views/Index/aa.vue')
      }]
    },
    {
      path: '/logon',
      name: 'logon',
      component: () => import('../views/Logon/logon.vue'),
    
    },
   
  ]
})
router.beforeEach((to,from,next)=>{             //路由跳转前 执行
  NProgress.start();                            //线 最上面 那条线 开始
 let {token,tokenExpired}=localStorage
 let currentTime=+new Date()
 if(to.meta.notDetect){
  next()
 }else{
  if(!token||tokenExpired<=currentTime){
    message.warn('您还未登录或未登录时间过长，请先登录')
    setTimeout(()=>{
      router.replace('/logon')
    },1000)
    
  }

    next()
  
 }
})
router.afterEach(()=>{                   //路由跳转后执行
  setTimeout(() => {
    NProgress.done();                //那条线   结束
  }, 300);
  
})
export default router
